<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'base.item.class.php');

class BaseItemOptionBase extends BaseItemBase {
	var $table = '';
	var $prefix = '';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $table_value = '';
	var $table_value_prefix = '';
	var $table_target = '';
	var $table_target_prefix = '';
	var $table_target_key = '';
	var $table_target_field = '';
	
	//
	var $_ovObj = NULL;
	
	//constructor
	function BaseItemOptionBase() {
		parent::BaseItemBase();
	}
	
	function setOvObj($ovObj) {
		$this->_ovObj = $ovObj;
	}
	
	//methods
	function getItemsValues($filter = '', $check = 0, $limit = true) {
		$ovs = array();
		$rows = $this->getItems($filter, $check, $limit);
		for($i=0, $n=sizeof($rows); $i<$n; $i++) {
			$ovs[] = array(
				'option' => $rows[$i],
				'values' => $this->getItemValues($rows[$i][$this->prefix . 'id']),
						);
		}
		
		return $ovs;
	}
	
	//option value
	function checkItemValue($ovid, $oid = '') {
		$params = $this->table_value_prefix . "id = '" . (int)$ovid . "'";
		if( $oid != '' ) {
			$params .= " and " . $this->prefix . "id = '" . (int)$oid . "'";
		}
		
		return $this->checkCountA($this->table_value, $params);
	}
	
	function getItemValue($ovid, $oid = '') {
		$query = "select * from " . $this->table_value . " where " . $this->table_value_prefix . "id = '" . (int)$ovid . "'";
		if( $oid != '' ) {
			$query .= " and " . $this->prefix . "id = '" . (int)$oid . "'";
		}
		
		return $this->adb->getRow($query);
	}
	
	function getItemValues($oid) {
		$query = "select * from " . $this->table_value . " where " . $this->prefix . "id = '" . (int)$oid . "' order by " . $this->table_value_prefix . "sort_order asc";
		$rows = $this->adb->getRows($query);
		
		return $rows;
	}
	
	function getItemValuesIDs($oid) {
		$ids = array();
		$options = $this->getItemValues($oid);
		if( is_array($options) ) {
			for($i=0, $n=sizeof($options); $i<$n; $i++) {
				$ids[] = $options[$i][$this->table_value_prefix . 'id'];
			}
		}
		
		return $ids;
	}
	
	function addItemValue($oid, $data_array) {
		$data_array[$this->prefix . 'id'] = $oid;
		$data_array[$this->table_value_prefix . 'date_added'] = 'now()';
		$data_array[$this->table_value_prefix . 'added_by'] = $this->_get_current_user();
		$data_array[$this->table_value_prefix . 'status'] = '1';
		
		$ovid = $this->adb->insertA($this->table_value, $data_array);
		
		return $ovid;
	}
	
	function addItemValues($oid, $data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		$data_array = func_db_input($data_array);
		for($i=0, $n=sizeof($data_array); $i<$n; $i++) {
			$value_data_array = array();
			
			if( is_array($data_array[$i]) ) {
				$value_data_array = $data_array[$i];
			} else {
				$value_data_array[$this->table_value_prefix . 'value'] = $data_array[$i];
			}
			$value_data_array[$this->prefix . 'id'] = $oid;
			$value_data_array[$this->table_value_prefix . 'date_added'] = 'now()';
			$value_data_array[$this->table_value_prefix . 'added_by'] = $this->_get_current_user();
			$value_data_array[$this->table_value_prefix . 'status'] = '1';
			
			$ovids[] = $this->adb->insertA($this->table_value, $value_data_array);
		}
		
		return $ovids;
	}
	
	function updateItemValue($oid, $ovid, $data_array) {
		if( $ovid == 0 ) {
			return $this->addItemValue($oid, $data_array);
		}
		$ids = $this->getItemValuesIDs($oid);
		if( in_array($ovid, $ids) ) {
			$value_data_array[$this->prefix . 'id'] = $oid;
			$value_data_array[$this->table_value_prefix . 'date_added'] = 'now()';
			$value_data_array[$this->table_value_prefix . 'added_by'] = $this->_get_current_user();
			$value_data_array[$this->table_value_prefix . 'status'] = '1';
			
			$params = $this->_query_owner_rt() . " and " . $this->prefix . "id = '" . (int)$ovid . "'";
			$this->adb->updateA($this->table_value, $value_data_array, $params);
		}
	}
	
	function updateItemValues($oid, $data_array) {
		$data_array = func_db_input($data_array);
		
		$option_data_array = $data_array['option'];
		$option_data_array[$this->prefix . 'last_modified'] = 'now()';
		$option_data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		$result = $this->adb->updateA($this->table, $option_data_array, "options_id = '" . (int)$oid . "'");
		
		//
		$idsb = array();
		$ids = $this->getItemValuesIDs($oid);
		for($i=0, $n=sizeof($data_array['values']); $i<$n; $i++) {
			if( is_array($data_array['values'][$i]) ) {
				$value_data_array = $data_array['values'][$i];
				if( $value_data_array[$this->table_value_prefix . 'id'] == 0 ) {
					$this->addItemValue($oid, $value_data_array);
				} elseif( in_array($value_data_array[$this->table_value_prefix . 'id'], $ids) ) {
					$value_data_array[$this->prefix . 'id'] = $oid;
					$value_data_array[$this->table_value_prefix . 'last_modified'] = 'now()';
					$value_data_array[$this->table_value_prefix . 'modified_by'] = $this->_get_current_user();
					$value_data_array[$this->table_value_prefix . 'status'] = '1';
					
					$params = $this->table_value_prefix . "id = '" . (int)$value_data_array[$this->table_value_prefix . 'id'] . "' " 
							. " and " . $this->prefix . "id = '" . (int)$oid . "'";
					$result = $this->adb->updateA($this->table_value, $value_data_array, $params);
					$idsb[] = $value_data_array[$this->table_value_prefix . 'id'];
				}
			}
		}
		if( sizeof($idsb) != sizeof($ids) ) {
			$a = array_values(array_diff($ids, $idsb));
			$this->adb->delete("delete from " . $this->table_value . " where " . $this->table_value_prefix . "id in (" . $this->_implode(',', $a) . ")");
		}
		
		return true;
	}
	
	function deleteItemValue($ovid, $oid = '') {
		$query = "delete " . $this->table_value . " from " . $this->table_value . " ov, " . $this->table_value . " o " 
				. " where " . $this->_query_owner('o.options_owner') . " and ov.options_id = o.options_id " 
				. " and ov." . $this->table_value_prefix . "id = '" . (int)$ovid . "' ";
		if( $oid == '' ) {
			$query .= "and o.options_id = '" . (int)$oid . "'";
		}
		
		return $this->adb->delete($query);
	}
	
	function deleteItemValues($oid) {
		$query = "delete " . $this->table_value . " from " . $this->table_value . " ov, " . $this->table_value . " o " 
				. " where " . $this->_query_owner('o.options_owner') . " and ov.options_id = o.options_id " 
				. " and ov.options_id = '" . (int)$oid . "' ";
		
		return $this->adb->delete($query);
	}
	
	//option and values
	function getItemAndValues($oid) {
		$ovs = array();
		$option = $this->getItem($oid);
		$values = $this->getItemValues($oid);
				
		$ovs = array('option' => $option, 'values' => $values);
		
		return $ovs;
	}
	
	function addItemAndValues($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);		
		$oid = $this->addItem($data_array['option']);
		$ovids = $this->addItemValues($oid, $data_array['values']);
		
		return $oid;
	}
	
	function updateItemAndValues($oid, $data_array) {
		$data_array = func_db_input($data_array);
		
		$option_data_array = $data_array['option'];
		$option_data_array[$this->prefix . 'last_modified'] = 'now()';
		$option_data_array[$this->prefix . 'modified_by'] = $this->_get_current_user();
		
		$result = $this->adb->updateA($this->table, $option_data_array, "options_id = '" . (int)$oid . "'");
		
		//
		$idsb = array();
		$ids = $this->getItemValuesIDs($oid);
		for($i=0, $n=sizeof($data_array['values']); $i<$n; $i++) {
			if( is_array($data_array['values'][$i]) ) {
				$value_data_array = $data_array['values'][$i];
				if( $value_data_array[$this->table_value_prefix . 'id'] == 0 ) {
					$this->addItemValue($oid, $value_data_array);
				} elseif( in_array($value_data_array[$this->table_value_prefix . 'id'], $ids) ) {
					$value_data_array[$this->prefix . 'id'] = $oid;
					$value_data_array[$this->table_value_prefix . 'last_modified'] = 'now()';
					$value_data_array[$this->table_value_prefix . 'modified_by'] = $this->_get_current_user();
					$value_data_array[$this->table_value_prefix . 'status'] = '1';
					
					$params = $this->table_value_prefix . "id = '" . (int)$value_data_array[$this->table_value_prefix . 'id'] . "' " 
							. " and " . $this->prefix . "id = '" . (int)$oid . "'";
					$result = $this->adb->updateA($this->table_value, $value_data_array, $params);
					$idsb[] = $value_data_array[$this->table_value_prefix . 'id'];
				}
			}
		}
		if( sizeof($idsb) != sizeof($ids) ) {
			$a = array_values(array_diff($ids, $idsb));
			$this->adb->delete("delete from " . $this->table_value . " where " . $this->table_value_prefix . "id in (" . $this->_implode(',', $a) . ")");
		}
		
		return true;
	}
	
	function deleteItemAndValues($oid) {
		$option = $this->getItem($oid);
		if( $option[$this->prefix . 'id'] > 0 ) {
			$query = "delete from " . $this->table . " where " . $this->prefix . "id = '" . (int)$oid . "'";
			$this->adb->delete($query);
			
			$query = "delete from " . $this->table_value . " where " . $this->prefix . "id = '" . (int)$oid . "'";
			$this->adb->delete($query);
			
			return true;
		}
		
		return false;
	}
	
	//
	function addTargetItemValue($target_id, $oid, $data_array) {
		return $this->_add_target_item_value($target_id, $oid, $ovid);
	}
	
	function updateTargetItemValue($target_id, $oid, $ovid) {
		$query = "select count(*) as count from " . $this->table_target . " " 
				. " where " . $this->table_target_field . " = '" . (int)$target_id . "' " 
				. " and attributes_unique = '" . (int)$oid . "' and attributes_valueunique = '" . (int)$ovid . "'";
		$count = $this->adb->getCount($query);
		if( $count > 0 ) {
			//
		} else {
			return $this->addTargetItemValue($target_id, $oid, $ovid);
		}
	}
	
	function deleteTargetItems($target_id) {
		$query = "delete from " . $this->table_target . " where " . $this->table_target_field . " = '" . (int)$target_id . "'";
		
		return $this->adb->delete($query);
	}
	
	function deleteTargetItem($target_id, $oid) {
		$query = "delete from " . $this->table_target . " where " . $this->table_target_field . " = '" . (int)$target_id . "' " 
				. " and attributes_unique = '" . (int)$oid . "'";
		
		return $this->adb->delete($query);
	}
	
	function deleteTargetItemValue($target_id, $oid, $ovid) {
		$query = "delete from " . $this->table_target . " where " . $this->table_target_field . " = '" . (int)$target_id . "' " 
				. " and attributes_unique = '" . (int)$oid . "' " 
				. " and attributes_valueunique = '" . (int)$ovid . "'";
		
		return $this->adb->delete($query);
	}
	
	//
	function _add_target_item_value($target_id, $oid, $ovid) {
		$data_array = array(
					$this->table_target_field => $target_id,
					$this->table_target_prefix . 'unique' => $oid,
					$this->table_target_prefix . 'valueunique' => $ovid,
						);
		return $this->adb->addA($this->table_target, $data_array);
	}
	
	function _update_target_item_value($tid, $target_id, $oid, $ovid) {
		$data_array = array(
					$this->table_target_field => $target_id,
					$this->table_target_prefix . 'unique' => $oid,
					$this->table_target_prefix . 'valueunique' => $ovid,
						);
		return $this->adb->updateA($this->table_target, $data_array, "attributes_id = '" . (int)$tid . "'");
	}
	
	//private
}
//
?>